package com.dxk.controller;


import com.dxk.dao.DepartmentDao;
import com.dxk.dao.EmployeeDao;
import com.dxk.pojo.Department;
import com.dxk.pojo.Employee;
import com.dxk.service.DepartmentService;
import com.dxk.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;

@Controller
public class loginController {

    //调用dao层
    private EmployeeDao emp;
    private DepartmentDao dao;



    @Autowired
    public void setDao(DepartmentDao dao) {
        this.dao = dao;
    }

    @Autowired
    public void setEmp(EmployeeDao emp) {
        this.emp = emp;
    }






    //查找所有部门，并且进入添加页面
    @RequestMapping("/add")
    public String ptm(Model model){

        Collection<Department> allDepartment = dao.getAllDepartment();
        model.addAttribute("dep",allDepartment);
        return "emp/add";
    }
    //登录界面
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession httpSession){


        if (username.equals("admin")&&password.equals("123456")){
            httpSession.setAttribute("user",password);
            System.out.println(username);
            return "redirect:/main.html";
        }
        model.addAttribute("hint","输入账号或密码有误");
      return "index";
    }

    //查询所有员工
    @RequestMapping("/user/list")
    public String List(Model model){
        Collection<Employee> list = emp.getAllEmployee();
        model.addAttribute("list",list);

        return "emp/list";
    }

    //添加员工   提交
    @RequestMapping("/user/add")
    public String add(Employee employee,Model model){


        System.out.println(employee);
        emp.addEmployee(employee);

        return "redirect:/user/list";
    }

    //删除一个员工
    @RequestMapping("user/del/{id}")
    public String del(@PathVariable("id") Integer id, Model model){
        System.out.println("员工的id是++++++++++++"+id);
        emp.deleteEmployee(id);

        return "redirect:/user/list";
    }



    //进入修改界面
    @GetMapping("/user/up/{id}")
    public String update(@PathVariable("id")int id, Model model){

        //获取所有部门，传递给前端，为了显示选择部门
        Collection<Department> allDepartment = dao.getAllDepartment();
        model.addAttribute("dep",allDepartment);
        //根据前端选择的员工id 来查找该员工
        Employee employee = emp.getEmployee(id);
        // 传递给前端，显示
        model.addAttribute("staff",employee);
        return "emp/update";
    }
    //提交修改
    @PostMapping("/user/ups")
    public String updateY(Integer id,Employee employee){

        System.out.println(id+"================"+employee);
        //  根据前端传的部门id获取部门
        Department departmentById = dao.getDepartmentById(employee.getDepartment().getId());
        //设置员工的部门
        employee.setDepartment(departmentById);
        //修改
        emp.updateEmployeeById(id,employee);

        return "redirect:/user/list";
    }


}
